package org.ala.distributed_transaction_commons.communication.participant;

import org.ala.distributed_transaction_commons.communication.Message;
import org.ala.distributed_transaction_commons.logs.LoggerFactory;
import org.ala.distributed_transaction_commons.redis.DistributedTransactionRedisTools;
import org.slf4j.Logger;
import org.springframework.beans.factory.InitializingBean;

import redis.clients.jedis.Pipeline;


/**
 * 向参与者publish消息
 *
 * @author 骆毅(ala)
 * @date 2024年2月18日
 */
public class ParticipantPublisher implements InitializingBean {
	
	
	static Logger log = LoggerFactory.getLogger("distributed_transaction/participant", "publisher");
	
	
	/**
	 * redis工具
	 */
	protected DistributedTransactionRedisTools distributedTransactionRedisTools;
	

	@Override
	public void afterPropertiesSet() throws Exception {
		
	}

	
	/**
	 * 向参与者发送can_commit消息
	 * @param	redisTx		redis事务操作对象
	 * @param	biz			业务类型
	 * @param	txId		事务id
	 */
	public void sendParticipantCanCommit(Pipeline redisTx, String biz, String txId) {
		String msg = Message.toParticipantCanCommit(biz, txId);
		redisTx.publish(Message.channelParticipant(txId), msg);
		log.info("[ParticipantPublisher sendParticipantCanCommit] 向参与者发送can_commit消息 biz:" + biz + " txId:" + txId);
	}
	public void sendParticipantCanCommit(String biz, String txId) {
		String msg = Message.toParticipantCanCommit(biz, txId);
		distributedTransactionRedisTools.withJedis(jedis -> jedis.publish(Message.channelParticipant(txId), msg));
		log.info("[ParticipantPublisher sendParticipantCanCommit] 向参与者发送can_commit消息 biz:" + biz + " txId:" + txId);
	}
	/**
	 * 向参与者发送pre_commit消息
	 * @param	redisTx		redis事务操作对象
	 * @param	biz			业务类型
	 * @param	txId		事务id
	 */
	public void sendParticipantPreCommit(Pipeline redisTx, String biz, String txId) {
		String msg = Message.toParticipantPreCommit(biz, txId);
		redisTx.publish(Message.channelParticipant(txId), msg);
		log.info("[ParticipantPublisher sendParticipantPreCommit] 向参与者发送pre_commit消息 biz:" + biz + " txId:" + txId);
	}
	public void sendParticipantPreCommit(String biz, String txId) {
		String msg = Message.toParticipantPreCommit(biz, txId);
		distributedTransactionRedisTools.withJedis(jedis -> jedis.publish(Message.channelParticipant(txId), msg));
		log.info("[ParticipantPublisher sendParticipantPreCommit] 向参与者发送pre_commit消息 biz:" + biz + " txId:" + txId);
	}
	/**
	 * 向参与者发送commit消息
	 */
	public void sendParticipantCommit(Pipeline redisTx, String biz, String txId) {
		String msg = Message.toParticipantCommit(biz, txId);
		redisTx.publish(Message.channelParticipant(txId), msg);
		log.info("[ParticipantPublisher sendParticipantCommit] 向参与者发送commit消息 biz:" + biz + " txId:" + txId);
	}
	public void sendParticipantCommit(String biz, String txId) {
		String msg = Message.toParticipantCommit(biz, txId);
		distributedTransactionRedisTools.withJedis(jedis -> jedis.publish(Message.channelParticipant(txId), msg));
		log.info("[ParticipantPublisher sendParticipantCommit] 向参与者发送commit消息 biz:" + biz + " txId:" + txId);
	}
	/**
	 * 向参与者发送finished消息
	 */
	public void sendParticipantFinished(Pipeline redisTx, String biz, String txId) {
		String msg = Message.toParticipantFinished(biz, txId);
		redisTx.publish(Message.channelParticipant(txId), msg);
		log.info("[ParticipantPublisher sendParticipantFinished] 向参与者发送finished消息 biz:" + biz + " txId:" + txId);
	}
	public void sendParticipantFinished(String biz, String txId) {
		String msg = Message.toParticipantFinished(biz, txId);
		distributedTransactionRedisTools.withJedis(jedis -> jedis.publish(Message.channelParticipant(txId), msg));
		log.info("[ParticipantPublisher sendParticipantFinished] 向参与者发送finished消息 biz:" + biz + " txId:" + txId);
	}
	/**
	 * 向参与者发送rollback消息
	 */
	public void sendParticipantRollback(Pipeline redisTx, String biz, String txId) {
		String msg = Message.toParticipantRollback(biz, txId);
		redisTx.publish(Message.channelParticipant(txId), msg);
		log.info("[ParticipantPublisher sendParticipantRollback] 向参与者发送rollback消息 biz:" + biz + " txId:" + txId);
	}
	public void sendParticipantRollback(String biz, String txId) {
		String msg = Message.toParticipantRollback(biz, txId);
		distributedTransactionRedisTools.withJedis(jedis -> jedis.publish(Message.channelParticipant(txId), msg));
		log.info("[ParticipantPublisher sendParticipantRollback] 向参与者发送rollback消息 biz:" + biz + " txId:" + txId);
	}


	public DistributedTransactionRedisTools getDistributedTransactionRedisTools() {
		return distributedTransactionRedisTools;
	}
	public void setDistributedTransactionRedisTools(DistributedTransactionRedisTools distributedTransactionRedisTools) {
		this.distributedTransactionRedisTools = distributedTransactionRedisTools;
	}
	
}
